package com.shunde.xunjian.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.shunde.xunjian.mapper.dbnetworkshunde",sqlSessionTemplateRef = "SqlSessionTemplatedbnetworkshunde")
public class DbnetworkshundeDataSourceConfig {
    // 获取映射文件所在的路径
//    @Value("${mybatis.dbnetworkshunde.mapper-locations}")
//    private String sqlMapperPath;

    @Autowired
    PaginationInterceptor paginationInterceptor;

    /**
     * @Author jason.tang
     * @Description: 根据配置文件，注入数据源
     * @Param []
     * @return javax.sql.DataSource
     */
    @Bean(name = "sqlDataSourcedbnetworkshunde")
    @ConfigurationProperties(prefix = "spring.datasource.dbnetworkshunde")
    public DataSource sqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * @Author jason.tang
     * @Description: 注入SqlSessionFactory，指定数据源和映射文件路径
     * @Param [dataSource]
     * @return org.apache.ibatis.session.SqlSessionFactory
     */
    @Bean(name = "SqlSessionFactorydbnetworkshunde")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("sqlDataSourcedbnetworkshunde") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        Interceptor[] plugins = {paginationInterceptor};
        mybatisSqlSessionFactoryBean.setPlugins(plugins);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setCallSettersOnNulls(true);
        return mybatisSqlSessionFactoryBean.getObject();
    }

    /**
     * @Author jason.tang
     * @Description: 注入DataSourceTransactionManager事物管理器
     * @Param [dataSource]
     * @return org.springframework.jdbc.datasource.DataSourceTransactionManager
     */
    @Bean(name = "sqlTransactionManagerdbnetworkshunde")
    public DataSourceTransactionManager transactionManagerbs(@Qualifier("sqlDataSourcedbnetworkshunde") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * @Author jason.tang
     * @Description: 注入SqlSessionTemplate模板
     * @Param [sqlSessionFactory]
     * @return org.mybatis.spring.SqlSessionTemplate
     */
    @Bean(name = "SqlSessionTemplatedbnetworkshunde")
    public SqlSessionTemplate sqlSessionTemplatebs(@Qualifier("SqlSessionFactorydbnetworkshunde") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
